#include <config.h>
#include <stdio.h>
#include <string.h>


static ItemCfg g_tItemCfgs[ITEM_MAX_NUM];
static int g_iItemCfgCount = 0;

/* 解析配置文件 */
int ParseConfig(void)
{
	FILE *fp;
	char buf[100];
	char *p;
	/* 打开文件 */
	fp = fopen(CFG_FILE, "r");
	if(fp == NULL)
	{
		printf("can't open cfg file: %s \n", CFG_FILE);
		return -1;
	}
	while(fgets(buf, sizeof(buf), fp))
	{
		/* read each line */
		//buf[99] = '\0';

		/* 吃掉开头的空格或者tab */
		p = buf;
		/* 忽略注释 */
		while(*p == ' ' || *p == '\t')
		{
			p++;
		}
		if(*p == '#')
		{
			continue;
		}
		/* 处理每一行 */
		g_tItemCfgs[g_iItemCfgCount].command[0] = '\0';
		g_tItemCfgs[g_iItemCfgCount].index = g_iItemCfgCount;
		sscanf(p, "%s %d %s", 	g_tItemCfgs[g_iItemCfgCount].name, 
								&g_tItemCfgs[g_iItemCfgCount].bCanBeTouched,
								g_tItemCfgs[g_iItemCfgCount].command);
		g_iItemCfgCount++;
	}
	return 0;
}


int GetItemCfgCount(void)
{
	return g_iItemCfgCount;
}
PItemCfg GetItemCfgByIndex(int index)
{
	if(index < g_iItemCfgCount)
	{
		return &g_tItemCfgs[index];
	}
	else
	{
		return NULL;
	}
}

PItemCfg GetItemCfgByName(char *name)
{
	int i;
	for(i = 0; i < g_iItemCfgCount; i++)
	{
		if(strcmp(g_tItemCfgs[i].name, name) == 0)
		{
			return &g_tItemCfgs[i];
		}
	}
	return NULL;
}


